class UniformesController < ApplicationController
  #MODULE: Uniformes

  # GET /uniformes
  # GET /uniformes.xml
  def index
    @uniformes = Uniforme.all
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @uniformes }
    end
  end

  # GET /uniformes/1
  # GET /uniformes/1.xml
  def show
    @uniforme = Uniforme.find(params[:id])
    respond_to do |format|
      format.html # show.html.erb
      format.js {render :layout=>false}
      format.xml  { render :xml => @uniforme }
    end
  end

  # GET /uniformes/new
  # GET /uniformes/new.xml
  def new
    @uniforme = Uniforme.new
    @uniforme.usuario_id=params[:usuario_id]
    #@uniforme.uniformes_vestimentas.build
    respond_to do |format|
      format.html # new.html.erb
      format.js { render :layout => false}
      format.xml  { render :xml => @formacion }
    end
  end

  # GET /uniformes/1/edit
  def edit
    @uniforme = Uniforme.find(params[:id])
    respond_to do |format|
      format.html # new.html.erb
      format.js { render :layout => false}
      format.xml  { render :xml => @formacion }
    end
    # @uniforme.uniformes_vestimentas.build
  end

  # POST /uniformes
  # POST /uniformes.xml
  def create
    @uniforme = Uniforme.new(params[:uniforme])
    @usuario = @uniforme.usuario
    respond_to do |format|
      if @uniforme.save
        format.html {
          flash[:notice] =  'Uniforme creado correctamente.'
          params[:oper].nil? ? redirect_to(@uniforme.usuario) : render(:text => "Ok")
        }
        format.js  { actualizar_view}
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @uniforme.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /uniformes/1
  # PUT /uniformes/1.xml
  def update
    @uniforme = Uniforme.find(params[:id])
    @usuario = @uniforme.usuario
    respond_to do |format|
      if @uniforme.update_attributes(params[:uniforme])
        format.html {
          flash[:notice] = 'Uniforme actualizado correctamente.'
          redirect_to(@uniforme.usuario)
        }
        format.js  { actualizar_view}
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @uniforme.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /uniformes/1
  # DELETE /uniformes/1.xml
  def destroy
    @uniforme = Uniforme.find(params[:id])
    if params[:vestimenta_id]
      @uniforme.vestimentas.delete(Vestimenta.find(params[:vestimenta_id]))
      respond_to do |format|
        format.js do
          render :update do |page|
            page["tr-uniformevestimenta-#{params[:tr]}"].remove
          end
        end
      end
    else
      @usuario = @uniforme.usuario
      @uniforme.destroy
      
      respond_to do |format|
        format.html {actualizar_view}
        format.js {@uniforme=nil}
      end
    end

  end

  private
  def actualizar_view
    @uniforme||=@usuario.uniforme
    @action_name="edit"
    render :update do |page|
      page.replace_html "uniformes", :file => "uniformes/index"
      page << "makeColorBox()"
    end
  end
   
end
